package com.heart.house.web.validation;

import com.heart.house.biz.service.UserService;
import com.heart.house.common.model.User;
import com.heart.house.common.result.ResultMsg;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author Luo Zhen
 * @create 2019-06-13 11:47
 */
@Component
public class UserHelper {

    @Autowired
    private UserService userService;

    public ResultMsg validate(User account) {
        if (StringUtils.isBlank(account.getEmail())) {
            return ResultMsg.errorMsg("邮箱不能为空");
        }
        // 查询邮箱是否重复
        boolean flag = userService.findByEmail(account.getEmail());
        if (flag) {
            return ResultMsg.errorMsg("邮箱已存在");
        }
        if (StringUtils.isBlank(account.getName())) {
            return ResultMsg.errorMsg("名字不能为空");
        }
        if (StringUtils.isBlank(account.getConfirmPasswd()) || StringUtils.isBlank(account.getPasswd())) {
            return ResultMsg.errorMsg("密码不能为空");
        }
        if (account.getPasswd().length() < 6) {
            return ResultMsg.errorMsg("密码大于6位");
        }
        return ResultMsg.successMsg("");
    }

}
